package com.incom.log.server.thread;

import com.incom.log.server.conf.ServiceConfig;
import com.incom.log.server.mq.LogService;
import com.incom.log.server.run.PrintLog;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class PringThread extends Thread {

    private static Logger log = LoggerFactory.getLogger(PringThread.class);

    private LogService service;

    public PringThread(LogService service) {
        this.service = service;
    }

    @Override
    public void run() {
        while(ServiceConfig.serverRunSts) {
            try {
                String read = service.read();
                if(read != null) {
                    service.write(read);
                } else {
                    Thread.sleep(500);
                }
            } catch (Exception e) {
                StringBuilder sb = new StringBuilder(e.getMessage()).append("\n");
                for (StackTraceElement stackTraceElement : e.getStackTrace()) {
                    sb.append(stackTraceElement.toString()).append("\n");
                }
                log.error(sb.toString());
            }
        }
    }
}
